/****************************************************************************
 * Copyright (c) 2008, 2009 Andreas Unger and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Andreas Unger - initial API and implementation 
 ****************************************************************************/

package org.eclipselabs.damos.diagram.core.internal.commands;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipselabs.damos.dml.DMLFactory;
import org.eclipselabs.damos.dml.Inlet;
import org.eclipselabs.damos.dml.Input;
import org.eclipselabs.damos.dml.Outlet;
import org.eclipselabs.damos.dml.Output;
import org.eclipselabs.damos.dml.Subsystem;
import org.eclipselabs.damos.dml.SubsystemInputPort;
import org.eclipselabs.damos.dml.SubsystemOutputPort;
import org.eclipselabs.damos.dml.SystemInterface;
import org.eclipselabs.damos.dml.util.DMLUtil;

public class ConfigureSubsystemCommand extends ConfigureElementCommand {
	
    public ConfigureSubsystemCommand(ConfigureRequest request, EClass configurableType) {
        super(request);
        setEClass(configurableType);
    }
    
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		ConfigureRequest request = (ConfigureRequest) getRequest();
		Subsystem subsystem = (Subsystem) request.getElementToConfigure();
    	
    	Input input = DMLFactory.eINSTANCE.createInput();
    	subsystem.getInputs().add(input);
    	Output output = DMLFactory.eINSTANCE.createOutput();
    	subsystem.getOutputs().add(output);
    	
    	SystemInterface systemInterface = (SystemInterface) request.getParameters().get(SystemInterface.class);
    	if (systemInterface != null) {
        	subsystem.setName(DMLUtil.findAvailableComponentName(subsystem.getOwningFragment(), systemInterface.getName()));
    		subsystem.getImplementedInterfaces().add(systemInterface);
    		for (Inlet inlet : systemInterface.getInlets()) {
    			SubsystemInputPort inputPort = DMLFactory.eINSTANCE.createSubsystemInputPort();
    			inputPort.setInlet(inlet);
    			input.getPorts().add(inputPort);
    		}
    		for (Outlet outlet : systemInterface.getOutlets()) {
    			SubsystemOutputPort outputPort = DMLFactory.eINSTANCE.createSubsystemOutputPort();
    			outputPort.setOutlet(outlet);
    			output.getPorts().add(outputPort);
    		}
    	} else {
    		subsystem.setName(DMLUtil.findAvailableComponentName(subsystem.getOwningFragment(), "Subsystem"));
    	}

		return CommandResult.newOKCommandResult(subsystem);
	}
	
}
